package com.aidex.web.controller.biz;

import com.aidex.biz.domain.AccountType;
import com.aidex.biz.service.AccountTypeService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统账户类型Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/biz/bizAccountType")
public class AccountTypeController extends BaseController {
    @Autowired
    private AccountTypeService accountTypeService;

    /**
     * 查询系统账户类型列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:list')")
    @GetMapping("/list")
    public R<PageInfo> list(AccountType accountType, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            accountType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        accountType.setPage(new PageDomain(request, response));
        return R.data(accountTypeService.findPage(accountType));
    }

    /**
     * 获取系统账户类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:query')")
    @GetMapping(value = "/{id}")
    public R<AccountType> detail(@PathVariable("id") String id) {
        return R.data(accountTypeService.get(id));
    }

    /**
     * 新增系统账户类型
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:add')")
    @Log(title = "系统账户类型", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated AccountType accountType) {
        return R.status(accountTypeService.save(accountType));
    }

    /**
     * 修改系统账户类型
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:edit')")
    @Log(title = "系统账户类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated AccountType accountType) {
        return R.status(accountTypeService.save(accountType));
    }


    /**
     * 删除系统账户类型
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:remove')")
    @Log(title = "系统账户类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(accountTypeService.deleteAccountTypeByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(accountTypeService.findMaxSort(new AccountType()));
    }

    /**
     * 导出系统账户类型列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountType:export')")
    @Log(title = "系统账户类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(AccountType accountType) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            accountType.setCompanyId(null);
        } else {
            accountType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<AccountType> list = accountTypeService.findList(accountType);
        ExcelUtil<AccountType> util = new ExcelUtil<AccountType>(AccountType.class);
        return util.exportExcel(list, "系统账户类型数据");
    }

    /**
     * 首页 左下角报表
     */

    @GetMapping("/getNowlist")
    public R getNowlist(AccountType accountType) {
        accountType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        return R.data(accountTypeService.getUserAccountInfo(accountType));
    }

}
